package com.lzy.huijinjiazu.mvp.ui.activity;

import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.gyf.barlibrary.ImmersionBar;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.lzy.huijinjiazu.R;
import com.lzy.huijinjiazu.app.EventBusTags;
import com.lzy.huijinjiazu.di.component.DaggerShoppingCartComponent;
import com.lzy.huijinjiazu.di.module.ShoppingCartModule;
import com.lzy.huijinjiazu.mvp.contract.ShoppingCartContract;
import com.lzy.huijinjiazu.mvp.presenter.ShoppingCartPresenter;
import com.lzy.huijinjiazu.mvp.ui.adapter.TabsAdapter;

import org.simple.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class ShoppingCartActivity extends BaseActivity<ShoppingCartPresenter> implements ShoppingCartContract.View {

    @BindView(R.id.tabLayout)
    SlidingTabLayout mTabLayout;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    @BindView(R.id.fl_title)
    FrameLayout mFlTitle;
    @BindView(R.id.tv_edit)
    TextView mTvEdit;
    private boolean luxuryEditState;
    private boolean ownEditState;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerShoppingCartComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .shoppingCartModule(new ShoppingCartModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_shopping_cart; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mPresenter.initTabLayout();
    }

    @Override
    public void showTabLayout(String[] title, Fragment[] tabFragment) {
        mViewPager.setAdapter(new TabsAdapter(getSupportFragmentManager(), tabFragment, title));
        mTabLayout.setViewPager(mViewPager);
        mTabLayout.setCurrentTab(0);
        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                if (position == 0) {
                    mTvEdit.setText(luxuryEditState ? "完成" : "编辑");
                } else {
                    mTvEdit.setText(ownEditState ? "完成" : "编辑");
                }
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }


    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.setTitleBarMarginTop(this, mFlTitle);
    }

    @OnClick({R.id.tv_edit, R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_edit:
                clickEdit();
                break;
            case R.id.iv_back:
                finish();
                break;
        }

    }

    private void clickEdit() {
        if (mTabLayout.getCurrentTab() == 0) {
            luxuryEditState = !luxuryEditState;
            mTvEdit.setText(luxuryEditState ? "完成" : "编辑");
        } else {
            ownEditState = !ownEditState;
            mTvEdit.setText(ownEditState ? "完成" : "编辑");
        }
        shoppingCartEvent(mTabLayout.getCurrentTab());
    }

    private void shoppingCartEvent(int position) {
        Message message = new Message();
        message.what = position;
        message.obj = position == 0 ? luxuryEditState : ownEditState;
        EventBus.getDefault().post(message, EventBusTags.SHOPPING_STATE);
    }
}
